package com.example.schoolenterprisealliance.activity;
//最新政策

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.schoolenterprisealliance.MainActivity;
import com.example.schoolenterprisealliance.R;
import com.example.schoolenterprisealliance.adapter.Hister_enventAdapter;
import com.example.schoolenterprisealliance.adapter.NavigationAdapter;
import com.example.schoolenterprisealliance.adapter.PolicyAdapter;
import com.example.schoolenterprisealliance.bean.History_event;
import com.example.schoolenterprisealliance.bean.Navigation;
import com.example.schoolenterprisealliance.bean.Policy;
import com.example.schoolenterprisealliance.dialog.MyDialog;
import com.example.schoolenterprisealliance.html.HtmlText;
import com.example.schoolenterprisealliance.io.AppConfig;
import com.example.schoolenterprisealliance.method.Skip;
import com.example.schoolenterprisealliance.service.MainService;
import com.example.schoolenterprisealliance.service.PolicyService;
import com.example.schoolenterprisealliance.time.Time;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class PolicyActivity extends AppCompatActivity {

    private PolicyAdapter policyAdapter;
    private RecyclerView recyclerView;
    private TextView tv_title;
    private TextView tv_context;
    private RecyclerView rv_navigation;
    private NavigationAdapter navigationAdapter;
    private Skip skip;//跳转
    private Intent intent;
    private EditText et_search;
    private ImageView iv_search;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_policy);

        recyclerView=findViewById(R.id.rv_poblic);
        View view = LayoutInflater.from(PolicyActivity.this).inflate(R.layout.item_information,null);
        tv_title=view.findViewById(R.id.tv_title);
        tv_context=view.findViewById(R.id.tv_context);
        rv_navigation=findViewById(R.id.rv_navigation);
        et_search = findViewById(R.id.et_search);
        iv_search = findViewById(R.id.iv_search);
        //一个是前一个activity的退出，另一个activity的进入。
        overridePendingTransition(R.animator.into_right, R.animator.out_left);



        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setOverScrollMode(View.OVER_SCROLL_NEVER);


        LinearLayoutManager linearLayoutManager1 = new GridLayoutManager(this, 5);
        linearLayoutManager1.setOrientation(LinearLayoutManager.VERTICAL);
        rv_navigation.setLayoutManager(linearLayoutManager1);
        navigationAdapter=new NavigationAdapter(R.layout.item_navigation, addNavigation());
        rv_navigation.setAdapter(navigationAdapter);
        policyData();
        navigationListener();
        searchIntent();
        ivImageIntent();
    }
    //获取政策数据
    private void policyData() {
        //1, 声明Retrofit 的构建者
        Retrofit.Builder builder = new Retrofit.Builder();
        //2, 设置公共的地址
        builder.baseUrl(AppConfig.BASE_URL);
        //3, 设置解析gson
        builder.addConverterFactory(GsonConverterFactory.create());
        //4, 得到Retrofit 对象
        Retrofit retrofit = builder.build();
        //5, 得到接口
        PolicyService policyService = retrofit.create(PolicyService.class);
        //6, 得到Call 的方法
        Call<Policy> call = policyService.getPolicyData();
        //7, 传入接口回调 , 获取数据
        call.enqueue(new Callback<Policy>() {
            @Override
            public void onResponse(Call<Policy> call, Response<Policy> response) {
                Policy policy=response.body();
                //数据源
                List<Policy.DataBean> data = policy.getData();
                PolicyAdapter policyAdapter = new PolicyAdapter(R.layout.item_information,data);
                recyclerView.setAdapter(policyAdapter);
                policyAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                        Intent intent=new Intent(PolicyActivity.this,ItemActivity.class);
                        intent.putExtra("title",data.get(position).getTitle());
                        intent.putExtra("time",data.get(position).getTime());
                        intent.putExtra("context",data.get(position).getContext());
                        intent.putExtra("layout","policy");
                        tv_title.setTextColor(Color.parseColor("#FF0000"));
                        tv_context.setTextColor(Color.parseColor("#FF0000"));
                        tv_title.setText("sdfsf");
                        recyclerView.setAdapter(policyAdapter);
                        Log.e("position",position+"");
                        startActivity(intent);
                    }
                });
                Log.e("history_events_response", response.body().toString());
            }

            @Override
            public void onFailure(Call<Policy> call, Throwable t) {
                Log.e("History_eventerror", t.getMessage());
                t.printStackTrace();
            }


        });
    }
    //添加导航栏数据
    private List<Navigation> addNavigation() {
        List<Navigation> data = new ArrayList<>();
        Navigation navigation;
        for (int i=0;i<5;i++){
            navigation=new Navigation();
            if (i==0){
                navigation.setId(i);
                navigation.setNavigetion("首页");
                data.add(navigation);
                continue;
            }else if (i==1){
                navigation.setId(i);
                navigation.setNavigetion("联盟介绍");
                data.add(navigation);
                continue;
            }else if (i==2){
                navigation.setId(i);
                navigation.setNavigetion("最新动态");
                data.add(navigation);
                continue;
            }else if (i==3){
                navigation.setId(i);
                navigation.setNavigetion("最新政策");
                data.add(navigation);
                continue;
            }else {
                navigation.setId(i);
                navigation.setNavigetion("互联网+");
                data.add(navigation);
                continue;
            }
        }
        return data;
    }
    //导航栏监听
    private void navigationListener(){
        navigationAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                TextView tv=view.findViewById(R.id.tv_navigation);
                String s=tv.getText().toString();
                if (s.equals("最新政策")){

                }else {
                    skip=new Skip(PolicyActivity.this,s);
                    intent=skip.intent();
                    startActivity(intent);
                }

                Log.e("navagationListener",tv.getText().toString());
            }
        });
    }
    //点击搜索框跳转
    private void searchIntent(){
        et_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(PolicyActivity.this, SearchHotActivity.class);
                startActivity(intent);
            }
        });
    }
    // 点击搜索图片跳转
    private void ivImageIntent(){
        iv_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(PolicyActivity.this,SearchHotActivity.class);
                startActivity(intent);
            }
        });
    }

}
